package shop.order.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import shop.common.core.Result;
import shop.common.entity.Order;
import shop.common.entity.OrderItem;
import shop.order.service.OrderService;

import java.util.List;

@RestController
@RequestMapping("/api/orders")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    @GetMapping("/test")
    public Result test() {
        return Result.ok().data("test");
    }

    @PostMapping("/create/{userId}")
    public Result create(@PathVariable("userId") String userId, @RequestParam String address, @RequestBody List<OrderItem> orderItemList) {
        boolean save = orderService.createOrder(userId,address, orderItemList);
        return save ? Result.ok() : Result.fail();
    }


    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable("id") String id) {
        boolean flag = orderService.removeById(id);
        return flag ? Result.ok() : Result.fail();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Order order) {
        boolean bool = orderService.updateById(order);
        return bool ? Result.ok() : Result.fail();
    }


    @GetMapping("/details/{id}")
    public Result details(@PathVariable String id) {
        Order order = orderService.details(id);
        return Result.ok().data(order);
    }

    @GetMapping("/list/{userId}")
    public Result listByUserId(@PathVariable("userId") String userId) {
        List<Order> list = orderService.listByUserId(userId);
        return Result.ok().data(list);
    }

    @GetMapping("/send/{id}")
    public Result send(@PathVariable("id") String id) {
        boolean update = orderService.update(Wrappers.lambdaUpdate(Order.class).eq(Order::getId, id).set(Order::getStatus, 1));
        return update ? Result.ok() : Result.fail();
    }

    @PostMapping("/list")
    public Result page(@RequestBody Order order, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        Page<Order> page = orderService.list(order, pageNum, pageSize);
        return Result.ok().data(page);
    }

}
